package com.xscha.tableforge.service.impl;

import com.xscha.tableforge.model.entity.OperationLog;
import com.xscha.tableforge.model.entity.Space;
import com.xscha.tableforge.service.OperationLogService;
import org.jooq.DSLContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import static com.xscha.tableforge.jooq.tables.OperationLogTable.OPERATION_LOG;
import static com.xscha.tableforge.jooq.tables.SpaceTable.SPACE;

@Service
public class OperationLogServiceImpl implements OperationLogService {

    private static final Logger logger = LoggerFactory.getLogger(OperationLogServiceImpl.class);

    @Autowired
    private DSLContext dsl;

    @Override
    public void save(OperationLog log) {
        dsl.insertInto(OPERATION_LOG)
            .set(OPERATION_LOG.ID, log.getId())
            .set(OPERATION_LOG.USER_ID, log.getUserId())
            .set(OPERATION_LOG.SPACE_ID, log.getSpaceId())
            .set(OPERATION_LOG.TABLE_ID, log.getTableId())
            .set(OPERATION_LOG.RECORD_ID, log.getRecordId())
            .set(OPERATION_LOG.OPERATION_TYPE, log.getOperationType())
            .set(OPERATION_LOG.OLD_DATA, log.getOldData())
            .set(OPERATION_LOG.NEW_DATA, log.getNewData())
            .set(OPERATION_LOG.IP, log.getIp())
            .set(OPERATION_LOG.USER_AGENT, log.getUserAgent())
            .set(OPERATION_LOG.CREATED_TIME, log.getCreatedTime())
            .execute();
    }
}